package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import com.sky.vo.OrdersVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    //像订单表中插入一条数据
    @Options(useGeneratedKeys = true , keyProperty = "id")
    @Insert("insert into orders(number, status, user_id, address_book_id, order_time, " +
            "checkout_time, pay_method, pay_status, amount, remark, phone, address, " +
            " consignee, estimated_delivery_time, delivery_status, pack_amount, " +
            "tableware_number, tableware_status) VALUE " +
            "(#{number} , #{status} , #{userId} , #{addressBookId} , #{orderTime} , " +
            "#{checkoutTime} , #{payMethod} , #{payStatus} , #{amount} , #{remark} , #{phone} , #{address} , " +
            "#{consignee} , #{estimatedDeliveryTime} , #{deliveryStatus} , #{packAmount} , " +
            "#{tablewareNumber} , #{tablewareStatus})")
    void insert(Orders order);

    //支付成功，修改订单状态
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    //更新
    void update(Orders orders);

    //动态查询
    Page<OrdersVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    //根据订单ID查询订单明细表
    @Select("select * from order_detail where order_id = #{id}")
    List<OrderDetail> getDish(Long id);

    //查询订单
    @Select("select * from orders where id = #{id}")
    OrderVO getById(Long id);

    @Select("select count(*) from orders where status = #{status}")
    Integer getCount(Integer status);

    @Select("select * from orders where user_id = #{userId}")
    Page<OrderVO> selectAll(Long userId);

    @Select("select * from orders where id = #{id}")
    Integer getStatusById(Long id);

    //根据订单状态和下单时间来查询订单
    @Select("select * from orders where status = #{status} and order_time <= #{orderTime}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

    //查询营业额
    Double sumByMap(Map<String, Object> map);

    //订单统计
    Integer countByMap(Map<String, Object> map);

    //销量前十
    List<GoodsSalesDTO> getSalesTop10(LocalDate begin, LocalDate end);
}
